#读取所有
from os import remove


def fun_read_all(name):
    f = open(name,'r')
    w = f.read()
    f.close()
    return w

#转移到studs
def transpot(name):
    f = open(name,'r')
    f2 = open('studs.txt', 'w')
    f2.write('')
    f2.close()
    f2 = open('studs.txt', 'a')
    while True:
        part1 = f.readline().split(":")
        part2 = f.readline().split(",")
        try:
            load = part1[0] + ':' + part2[1]
            f2.write(load)
        except:
            f.close()
            f2.close()
            break
    return '转移成功'

#找到最大
def highest(name):
    transpot(name)
    f = open('studs.txt', 'r')
    group_ = f.readlines()
    max_ = []
    for i in group_:
        part = i.split(":")
        max_.append(int(part[1]))
    max_number = max(max_)
    position = max_.index(max_number)
    # position = [index for index, value in enumerate(max_) if value == max_number]
    output = group_[position]
    f.close()
    return output

def class_average(name):
    list_class = []
    list_num = []
    num_e = num_m = num_c = num_k = num_E = 0
    num_e1 = num_m1 = num_c1 = num_k1 = num_E1 = 0
    f = open(name, 'r')

    while True:
        part = f.readline().split(":")
        try:
            list_class.append(part[1])
        except:
            f.close()
            break

    for i in list_class:
        part2 = i.split(",")
        if len(part2) > 1:  # 确保 part2 至少有两个元素
            list_num.append(int(part2[1]))  # 将字符串转换为整数

    for i in list_class:
        if '经济' in i:
            num_e += list_num[list_class.index(i)]
            num_e1 += 1
        elif '机械' in i:
            num_m += list_num[list_class.index(i)]
            num_m1 += 1
        elif '计算' in i:
            num_c += list_num[list_class.index(i)]
            num_c1 += 1
        elif '会计' in i:
            num_k += list_num[list_class.index(i)]
            num_k1 += 1
        elif '英语' in i:
            num_E += list_num[list_class.index(i)]
            num_E1 += 1

    result_e = round(num_e / num_e1,2)
    result_m = round(num_m / num_m1,2)
    result_c = round(num_c / num_c1,2)
    result_k = round(num_k / num_k1,2)
    result_E = round(num_E / num_E1,2)
    f.close()
    print ('经济191:',result_e, '\n'
          '机械191:',result_m, '\n'
          '计算191:',result_c, '\n'
          '会计191:',result_k, '\n'
          '英语191:',result_E, '\n')


def find_stu(name,stu_name):
    f = open(name, 'r')
    load = f.readlines()
    for i in load:
        if i.find(stu_name,0,3)>-1:
            position = load.index(i)
            break
    else:
        raise TypeError('没有找到')
    f.close()
    try:
        return load[position]
    except:
        return '我也不知道发生了什么'


def change(name,stu_name,new_score):
    try:
        student = find_stu(name,stu_name)
    except:
        return '没有找到'

    f = open(name, 'r')
    load = f.readlines()
    position = load.index(student)
    part = load.pop(position).split(',')
    load.insert(position,part[0]+','+str(new_score)+'\n')
    f.close()
    remove('data.txt')
    f = open('data.txt','w')
    for i in load:
        f.write(i)
    f.close()
    return '修改成功'

if __name__ == '__main__':
    print("输入你要进行的操作：\n"
          "1，查看当前名单\n"
          "2，创建列表——学生：成绩\n"
          "3，当前最高分\n"
          "4，各班平均分\n"
          "5，查找学生\n"
          "6，修改学生成绩\n")
    choice_ = input()
    if choice_ == '1':
        print(fun_read_all('data.txt'))
    elif choice_ == '2':
        print(transpot('data.txt'))
    elif choice_ == '3':
        print(highest('data.txt'))
    elif choice_ == '4':
        class_average('data.txt')
    elif choice_ == '5':
        find_ = input('你要找谁')
        print(find_stu('data.txt',find_))
    elif choice_ == '6':
        find_ = input('你要改谁的成绩')
        print(find_stu('data.txt',find_))
        score = input('改为多少？')
        print(change('data.txt',find_,score))